C ] How can I handle weird errors from calculating acos / sin / atan2?
Posted
by Phrixus
on Stack Overflow
See other posts from Stack Overflow
or by Phrixus
Published on 2010-03-25T08:14:49Z
Indexed on
2010/03/25
8:33 UTC
Read the original article
Hit count: 274
Has anyone seen this weird value while handling sin / cos/ tan / acos.. math stuff?
===THE WEIRD VALUE===
-1.#IND00
=====================
void inverse_pos(double x, double y, double& theta_one, double& theta_two)
{
// Assume that L1 = 350 and L2 = 250
double B = sqrt(x*x + y*y);
double angle_beta = atan2(y, x);
double angle_alpha = acos((L2*L2 - B*B - L1*L1) / (-2*B*L1));
theta_one = angle_beta + angle_alpha;
theta_two = atan2((y-L1*sin(theta_one)), (x-L1*cos(theta_one)));
}
This is the code I was working on.
In a particular condition - like when x & y are 10 & 10, this code stores -1.#IND00 into theta_one & theta_two.
It doesn't look like either characters or numbers :(
Without a doubt, atan2 / acos / stuff are the problems.
But the problem is, try and catch doesn't work either cuz those double variables have successfully stored some values in them.
Moreover, the following calculations never complain about it and never break the program!
I'm thinking of forcing to use this value somehow and make the entire program crash... So that I can catch this error..
Except for that idea, I have no idea how I should check whether these theta_one and theta_two variables have stored this crazy values.
Any good ideas?
Thank you in advance..
© Stack Overflow or respective owner